package com.nantian.main;

import com.nantian.office.excel.Excel;
import com.nantian.office.excel.ExcelUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.*;

/**
 * <p>ClassName: Main</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright©2014</p>
 * <p>Company: 广州南天电脑系统有限公司</p>
 * <p>Date: 2020-01-21</p>
 *
 * @author 刘晓辉
 * @version 1.0
 */
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        byte []out = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                                 (byte)0x10, (byte)0x11, (byte)0x12, (byte)127, (byte)128, (byte)255 };


         System.out.println( bytesToHexString((byte)10));
         System.out.println( bytesToHexString(out) );
         System.out.println( bytesToHexString(out, 0) );
         System.out.println( bytesToHexString(out, 1) );

         byte []b1 = hexStringToBytes("0123456789ABCDEF");
         for( int i = 0; i < b1.length; i++ ) {
                 System.out.println("[" + i + "]:" + b1[i]);
             }
         System.out.println( bytesToHexString(b1) );

         byte []b2 = bcdStringToBytes("1234567890123456");
         for( int i = 0; i < b2.length; i++ ) {
                 System.out.println("[" + i + "]:" + b2[i]);
             }
         System.out.println( bytesToHexString(b2) );

         byte []bcdBytes = {0, 1, 2, 3, 4, 5, 6, 7, 9, 111,
                         10, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99};
         System.out.println( bcdToHexString(bcdBytes) );
    }

    public static String bytesToHexString(byte src) {
         int v = src & 0xFF;
         String hv = Integer.toHexString(v);
         return hv;
     }

      public static String bytesToHexString(byte[] src) {
         StringBuilder stringBuilder = new StringBuilder("");
         if (src == null || src.length <= 0) {
                 return null;
             }
         for (int i = 0; i < src.length; i++) {
                 int v = src[i] & 0xFF;
                 String hv = Integer.toHexString(v);
                 hv = hv.toUpperCase();
                 if (hv.length() < 2) {
                         stringBuilder.append(0);
                     }
                 stringBuilder.append(hv);
             }
         return stringBuilder.toString();
     }

      public static String bytesToHexString(byte[] src, int mode) {
         StringBuilder stringBuilder = new StringBuilder("");
         if (src == null || src.length <= 0) {
                 return null;
             }
         for (int i = 0; i < src.length; i++) {

                 if( i != 0 && i % 16 == 0 ) {
                         stringBuilder.append("\r\n");
                     }

                 if( mode == 1 ) {
                         stringBuilder.append("0x");
                     }

                 int v = src[i] & 0xFF;
                 String hv = Integer.toHexString(v);
                 if (hv.length() < 2) {
                         stringBuilder.append(0);
                     }
                 stringBuilder.append(hv);

                 if( mode == 1 && i+1 != src.length ) {
                         stringBuilder.append(',');
                     }
                 stringBuilder.append(" ");
             }
         return stringBuilder.toString();
     }

      public static String bcdToHexString(byte[] src) {
         StringBuilder stringBuilder = new StringBuilder("");
         if (src == null || src.length <= 0) {
                 return null;
             }
         for (int i = 0; i < src.length; i++) {
                 int v = src[i] & 0xFF;
                 int v1 = v / 10;
                 int v2 = v % 10;

                 if( v >= 0 && v < 100 ) {
                         stringBuilder.append( v1 );
                         stringBuilder.append( v2 );
                     }
             }
         return stringBuilder.toString();
     }

     public static byte[] hexStringToBytes(String hexString) {
         if (hexString == null || hexString.equals("") || hexString.length() % 2 != 0) {
                 return null;
             }
         hexString = hexString.toUpperCase();
         int length = hexString.length() / 2;
         char[] hexChars = hexString.toCharArray();
         byte[] d = new byte[length];
         for (int i = 0; i < length; i++) {
                 int pos = i * 2;
                 d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
             }
         return d;
     }

     public static byte[] bcdStringToBytes(String hexString) {
         if (hexString == null || hexString.equals("") || hexString.length() % 2 != 0) {
                 return null;
             }
         hexString = hexString.toUpperCase();
         int length = hexString.length() / 2;
         char[] hexChars = hexString.toCharArray();
         byte[] d = new byte[length];
         for (int i = 0; i < length; i++) {
                 int pos = i * 2;
                 d[i] = (byte) (charToByte(hexChars[pos]) * 10 + charToByte(hexChars[pos + 1]));
             }
         return d;
     }

     private static byte charToByte(char c) {
         return (byte) "0123456789ABCDEF".indexOf(c);
     }

    public static String DecimaltoBcd(String str){
        String b_num="";
        for (int i = 0; i < str.length(); i++) {

            String b = Integer.toBinaryString(Integer.parseInt(str.valueOf(str.charAt(i))));

            int b_len=4-b.length();

            for(int j=0;j<b_len;j++){
                b="0"+b;
            }
            b_num+=b;
        }

        return b_num;
    }

    public static String HextoBcd(String hex){

        int decimal = Integer.parseInt(hex,16);
        System.out.println(decimal);
        String bcd = DecimaltoBcd(String.valueOf(decimal));

        return bcd;
    }



    public static void test() {
        for (int i = 0; i < 10; i++) {
            long start = System.currentTimeMillis();
            String path = "/Users/liuxiaohui/Desktop/导入名单.xlsx";
            List<Map<String, Object>> data = ExcelUtils.parseObject(path, 1, Integer.MAX_VALUE, Integer.MAX_VALUE);
            System.out.println("总行数" + data.size() + ",总列数：" + data.get(0).size());
            System.out.println(data);
            long mid = System.currentTimeMillis();
            System.out.println("第[" + i + "]读取excel数据消耗：" + (mid - start) + "毫秒");
            List<User> data1 = ExcelUtils.parseObject(path, 1, User.class, Integer.MAX_VALUE, Integer.MAX_VALUE);
            System.out.println(data1);
            long end = System.currentTimeMillis();
            System.out.println("第[" + i + "]转换对象消耗：" + (end - mid) + "毫秒");
            System.out.println("第[" + i + "]总消耗：" + (end - start) + "毫秒");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void test2() throws FileNotFoundException {
        String path = "/Users/liuxiaohui/Desktop/5.xlsx";
        String temp = "/Users/liuxiaohui/Desktop/3.xlsx";
        FileOutputStream out = new FileOutputStream(new File(path), true);

        FileInputStream is = new FileInputStream(new File(path));

        Map<String, Object> map = new HashMap<>();
        map.put("name", "刘晓辉");
        map.put("age", 19);
        List<Map<String, Object>> data = new ArrayList<>();
        data.add(map);

        LinkedHashMap headers = new LinkedHashMap();
        headers.put("name", "姓名");
        headers.put("age", "年龄");

        Map<String, Integer> columnindex = new HashMap<>();
        columnindex.put("name", 1);
        columnindex.put("age", 4);

        Excel.mapToExcelFile(path, temp, data, null, columnindex, null, 5);


        User user = new User();
        user.setAge(19);
        user.setDate("2020-02-10");
        user.setDatetime(new Date());
        user.setHid(true);
        user.setLongValue(989898893289L);
        user.setManery(33.33f);
        user.setName("刘晓辉");
        user.setSex(1);
        user.setSource(55.55);
        user.setTime("2020-10-20 14:33:21");
        List<User> users = new ArrayList<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            users.add(user);
        }
        long mid = System.currentTimeMillis();
        System.out.println("添加数据消耗" + (mid - start)  + "毫秒");
//        ExcelUtils.objectToExcelFile(out,1, users, false);
        long end = System.currentTimeMillis();

        System.out.println("写入数据消耗" + (end - mid)  + "毫秒");
    }
}
